#ifndef __BSP_FLASH__
#define __BSP_FLASH__

#ifdef __cplusplus
extern "C" {
#endif

#include "bsp_config.h"

// #define BSP_FLASH_UNIT_TEST_EN
#ifdef BSP_FLASH_UNIT_TEST_EN
    #define BSP_FLASH_CONDITION_TEST
#endif

#define ADDR_FLASH_SECTOR_NUM   12

//FLASH sector start address, mcu have 0-11 sector
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) 	//sector0, 16 Kbytes  
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) 	//sector1, 16 Kbytes  
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) 	//sector2, 16 Kbytes  
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) 	//sector3, 16 Kbytes  
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) 	//sector4, 64 Kbytes  
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) 	//sector5, 128 Kbytes  
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) 	//sector6, 128 Kbytes  
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) 	//sector7, 128 Kbytes  
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) 	//sector8, 128 Kbytes  
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) 	//sector9, 128 Kbytes  
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) 	//sector10,128 Kbytes  
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) 	//sector11,128 Kbytes

#define BSP_FLASH_START         FLASH_BASE
#define BSP_FLASH_SIZE          0x100000            // 1M
#define BSP_FLASH_PAGE_SIZE     0x1000              // 4K
#define BSP_UNLOCK_PE_KEY       0xA9B8C7D6          /*!< unlock page erase function key */

#define ADDR_FLASH_END          (FLASH_BASE+0x100000)

#define BSP_WAIT_OPERATION_TIME                                 100                 //ms
#define BSP_WAIT_SECTOR_ERASE_OPERATION_TIME                    10000 

typedef enum
{
    BSP_FLASH_ERROR_NULL = 0,
    BSP_FLASH_ERROR_PAREMETER,
    BSP_FLASH_ERROR_OVERRANGE,
    BSP_FLASH_ERROR_ERASE_ALIGN,
    BSP_FLASH_ERROR_UNLOCK,
    BSP_FLASH_ERROR_WAIT_OPT,
    BSP_FLASH_ERROR_WRITE_FAILED,
    BSP_FLASH_ERROR_ERASE_SECTOR_START,
    BSP_FLASH_ERROR_ERASE_SECTOR_END,
    BSP_FLASH_ERROR_TEST_CHECK,

}en_BspErr;

int8_t bsp_flash_sector_erase(uint32_t addr,uint32_t len);
int8_t bsp_flash_erase(uint32_t addr,uint32_t len);
int8_t bsp_flash_write(uint32_t addr,uint8_t *data,uint32_t len);
int8_t bsp_flash_read(uint32_t addr,uint8_t *data,uint32_t len);
int8_t bsp_flash_erase_and_write(uint32_t addr,uint8_t *data,uint32_t len,uint8_t is_end_keep);

#ifdef BSP_FLASH_UNIT_TEST_EN
int8_t bsp_flash_test(const uint32_t addr,const uint32_t len);
void bsp_flash_unit_test(void);
#endif

#ifdef __cplusplus
}
#endif

#endif